package com.cly.item.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cly.common.entity.item.dto.ItemDTO;
import com.cly.common.entity.item.dto.ScheduleItemDTO;
import com.cly.common.entity.order.dto.OrderDetailDto;
import com.cly.common.entity.item.po.Item;
import com.cly.common.entity.page.PageBean;
import com.cly.common.entity.result.Result;
import com.cly.common.exception.CommonException;
import com.cly.item.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 通过数据库分页查询商品
     * @param pageNum
     * @param pageSize
     * @param name
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/public/queryPage")
    public Result queryItemByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "status", required = false) Integer status
    ){
        Page page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), Item::getItemName, name)
                        .eq(categoryId != null, Item::getCategoryId, categoryId)
                                .eq(status != null, Item::getStatus, status);
        Page pageItem = itemService.page(page, queryWrapper);
        return Result.success(new PageBean<>(pageItem.getTotal(), pageItem.getRecords()));
    }

    /**
     * 新增商品
     * @param itemDTO
     * @return
     */
    @PostMapping("/add")
    public Result addItem(@RequestBody ItemDTO itemDTO) {
        try {
            itemService.save(BeanUtil.copyProperties(itemDTO,Item.class));
            return Result.success();
        } catch (Exception e) {
            throw new CommonException("新增商品失败", e);
        }
    }

    /**
     * 根据ids查询商品
     * @param ids
     * @return
     */
    @GetMapping("/public/getByIds")
    public List<Item> queryItemsByIds(@RequestParam("ids") List<Long> ids){
        return itemService.queryItemsByIds(ids);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/public/get/{id}")
    public Result getItemById(@PathVariable("id") Long id){
        try {
            Item item = itemService.getById(id);
            return Result.success(item);
        } catch (Exception e) {
            log.info("根据id查询商品失败");
            throw new CommonException("查询失败",e);
        }
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteItem(@PathVariable("id") String id){
        try {
            itemService.removeById(id);
            return Result.success();
        } catch (Exception e) {
            throw new CommonException("删除商品失败", e);
        }
    }

    /**
     * 更新商品
     * @param itemDTO
     * @return
     */
    @PutMapping("/update")
    public Result updateItem(@RequestBody ItemDTO itemDTO){
        try {
            itemService.updateById(BeanUtil.copyProperties(itemDTO,Item.class));
            return Result.success();
        } catch (Exception e) {
            throw new CommonException("更新商品失败", e);
        }
    }

    /**
     * 扣减库存
     * @param OrderDetailDtoList
     * @return
     */
    @PutMapping("/stock/deduct")
    public void deductStock(@RequestBody List<OrderDetailDto> OrderDetailDtoList){
       itemService.deductStock(OrderDetailDtoList);
    }

    /**
     *  恢复库存
     * @param OrderDetailDtoList
     */
    @PutMapping("/recover/deduct")
    public void recoverStock(@RequestBody List<OrderDetailDto> OrderDetailDtoList){
        itemService.recoverStock(OrderDetailDtoList);
    }

}
